package com.jsz.peini.redis.util;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.redis.base.RedisConstants;
import com.jsz.peini.redis.base.RedisUtil;

/**
 * @author Han Guohong
 * @version 2017-3-17 上午11:10:06
 * 类说明
 */
public class SignRedisUtil {
	
	/**
	 * 更新用户最新签到日期信息
	 * @author Han Guohong
	 * @param userId
	 * @param date
	 */
	public static void updateLastSignDate(String userId,Date date){
		RedisUtil.hset(RedisConstants.SIGN_DATE.getBytes(), userId.getBytes(), DateTimeUtils.formatDateTime2String(date, "yyyy-MM-dd").getBytes());
	}
	
	/**
	 * 获取用户最后的签到日期
	 * @author Han Guohong
	 * @param userId
	 * @return
	 */
	public static String getLastSignDate(String userId){
		byte [] strArr = RedisUtil.hget(RedisConstants.SIGN_DATE.getBytes(), userId.getBytes());
		if(strArr == null){
			return null;
		}else{
			return new String(strArr);
		}
	}
	
	/**
	 * 更新用户签到提醒日期
	 * @author Han Guohong
	 * @param userId
	 * @param date
	 */
	public static void updateLastSignRemind(String userId,Date date){
		RedisUtil.hset(RedisConstants.SIGN_REMIND.getBytes(), userId.getBytes(), DateTimeUtils.formatDateTime2String(date, "yyyy-MM-dd").getBytes());
	}
	
	/**
	 * 获取是否需要提醒用户签到
	 * @author Han Guohong
	 * @param userId
	 * @return
	 */
	public static boolean ifRemind(String userId){
		byte [] strArr = RedisUtil.hget(RedisConstants.SIGN_DATE.getBytes(), userId.getBytes());
		if(strArr == null){//没有签到信息，查询缓存中是否存在签到提醒信息
			//return true;
			strArr = RedisUtil.hget(RedisConstants.SIGN_REMIND.getBytes(), userId.getBytes());
			if(strArr == null){
				return true;
			}else{
				String date = new String(strArr);
				if(StringUtils.isBlank(date)){
					return true;
				}else{
					String now = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd");
					if(now.equals(date)){
						return false;
					}else{
						return true;
					}
				}
			}
		}else{
			String date = new String(strArr);
			if(StringUtils.isBlank(date)){
				return true;
			}else{
				String now = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd");
				if(now.equals(date)){
					return false;
				}else{
					
					strArr = RedisUtil.hget(RedisConstants.SIGN_REMIND.getBytes(), userId.getBytes());
					if(strArr == null){
						return true;
					}else{
						date = new String(strArr);
						if(StringUtils.isBlank(date)){
							return true;
						}else{
							now = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd");
							if(now.equals(date)){
								return false;
							}else{
								return true;
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * 获取连续签到天数
	 * @author Han Guohong
	 * @param userId
	 */
	public static Integer getSignDays(String userId){
		int cnt = 0;
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		if(day == 1){//每月1日重新开始
			String lastD = SignRedisUtil.getLastSignDate(userId);
			if(StringUtils.isBlank(lastD)){
				RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "0".getBytes());
				cnt = 0;
				return cnt;
			}
			
			String nowStr = DateTimeUtils.formatDateTime2ShortString(new Date());
			if(lastD.equals(nowStr)){
				RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "1".getBytes());
				cnt = 1;
			}else{
				RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "0".getBytes());
				cnt = 0;
			}
		}else{
			//查询上一次签到记录
			String lastD = SignRedisUtil.getLastSignDate(userId);
			if(StringUtils.isBlank(lastD)){
				RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "0".getBytes());
				cnt = 0;
				return cnt;
			}
			
			String nowStr = DateTimeUtils.formatDateTime2ShortString(new Date());
			if(lastD.equals(nowStr)){//今天签到过，进一步判断是否跟昨天一样
				/*calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-1);
				String yestd = DateTimeUtils.formatDateTime2String(calendar.getTime(), "yyyy-MM-dd");
				if(lastD.equals(yestd)){*/
					byte [] str = RedisUtil.hget(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes());
					if(str == null){
						RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "0".getBytes());
						cnt = 0;
					}else{
						Integer days  = Integer.parseInt(new String(str));
						//days ++ ;
						RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), String.valueOf(days).getBytes());
						cnt = days;
					}
				/*}else{
					RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "1".getBytes());
					cnt = 1;
					return cnt;
				}*/
			}else{//是否跟昨天一样
				calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-1);
				String yestd = DateTimeUtils.formatDateTime2String(calendar.getTime(), "yyyy-MM-dd");
				if(lastD.equals(yestd)){//跟昨天一样
					byte [] str = RedisUtil.hget(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes());
					if(str == null){
						RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "0".getBytes());
						cnt = 0;
					}else{
						Integer days  = Integer.parseInt(new String(str));
						//days ++ ;
						RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), String.valueOf(days).getBytes());
						cnt = days;
					}
				}else{
					RedisUtil.hset(RedisConstants.SIGN_DAYS.getBytes(), userId.getBytes(), "0".getBytes());
					cnt = 0;
				}
			}
		}
		return cnt;
	}
}
